import {handleActions} from 'redux-actions';

import { ORDER_APPLY, ORDERS_QUERY, ORDER_QUERY, TIMELINE_QUERY, ORDER_CHECK, ORDER_APPLY_FINISH, CHECK_ONE_UNMOUNT } from '../actions/ActionTypes.js'

const order = handleActions({
  [ORDER_APPLY.REQUEST](state) {
    return {...state, loading: true};
  },
  [ORDER_APPLY.SUCCESS](state, action) {
    return {...state, success:true, loading: false};
  },
  [ORDER_APPLY.FAILURE](state, action) {
    return {...state, err: action.err, loading: false};
  },
  [ORDER_APPLY_FINISH](state, action) {
    return {...state, success: false, loading:false,
      err:{},
      orders:[],
      company:{},
      timeLines:[],
      result:null};
  },
  [ORDERS_QUERY.REQUEST](state) {
    return {...state, loading: true};
  },
  [ORDERS_QUERY.SUCCESS](state, action) {
    return {...state, orders:action.payload, loading: false};
  },
  [ORDERS_QUERY.FAILURE](state, action) {
    return {...state, err: action.err, loading: false};
  },
  [ORDER_QUERY.REQUEST](state) {
    return {...state, loading: true};
  },
  [ORDER_QUERY.SUCCESS](state, action) {
    return {...state, company:action.payload, loading: false};
  },
  [ORDER_QUERY.FAILURE](state, action) {
    return {...state, err: action.err, loading: false};
  },
  [TIMELINE_QUERY.REQUEST](state) {
    return {...state, loading: true};
  },
  [TIMELINE_QUERY.SUCCESS](state, action) {
    return {...state, timeLines:action.payload, loading: false};
  },
  [TIMELINE_QUERY.FAILURE](state, action) {
    return {...state, err: action.err, loading: false};
  },
  [ORDER_CHECK.REQUEST](state) {
    return {...state, loading: true};
  },
  [ORDER_CHECK.SUCCESS](state, action) {
    return {...state, result: action.payload, loading: false};
  },
  [ORDER_CHECK.FAILURE](state, action) {
    return {...state, err: action.err, loading: false};
  },
  [CHECK_ONE_UNMOUNT](state, action) {
    return {...state, company:{}, timeLines:[], loading:false, result:null};
  },


}, {
  loading:false,
  err:{},
  orders:[],
  company:{},
  timeLines:[],
  result:null,
  success:false,
});

export default order;
